package com.drop.action;

import com.drop.common.MyComment;
import com.drop.common.Util;
import com.drop.entity.Order;
import com.drop.service.CommentService;
import com.drop.service.OrderShowService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 璇勮琛ㄧ幇灞� * Created by 闄堜笢涓�on 2017/9/23 11:55
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/comment")
@Scope("prototype")
public class CommentAction extends ActionSupport{
	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private CommentService commentService;
	@Autowired
	private OrderShowService orss;

	/**
	 * 显示评论
	 * @return
	 * @throws Exception
	 */
	@Action(value = "list",results = {})
	public String showComments() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String id = request.getParameter("itemid");
		List<MyComment> myComments = commentService.showComments(Integer.parseInt(id));
		log.info(myComments);
		Util.returnJson(response,myComments);
		return null;
	}


	@Action(value = "jump",results = {
			@Result(name = "jump", location = "/WEB-INF/person/commentlist.jsp")
	})
	public String jump() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int orderid=Integer.parseInt(request.getParameter("orderId"));
		Order order=orss.getOrderById(orderid);
		ActionContext.getContext().put("order", order);
		return "jump";
	}

	/**
	 * 添加评论
	 * @return
	 */
	@Action(value = "add",results = {
			@Result(name = "addcomment", location = "com",type = "chain")
	})
	public String addcomment() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String []comment=request.getParameterValues("comment");
		String []gids=request.getParameterValues("gids");
		int orderid=Integer.parseInt(request.getParameter("orderid"));
		Integer uid= (Integer) request.getSession().getAttribute("id");
		commentService.addComment(comment,orderid,uid.toString(),gids);
		return "addcomment";
	}
}
